package com.tenglong.teacher.ui.setting.setting;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.TextView;

import com.cuotibao.zhuoxiao.basics.GlobalConstant;
import com.cuotibao.zhuoxiao.basics.utils.clean.DataCleanManager;
import com.tenglong.teacher.R;
import com.tenglong.teacher.ui.DebugActivity;
import com.tenglong.teacher.ui.common.activity.BaseActivity;
import com.tenglong.teacher.ui.dialog.DebugDialogFragment;

public class SettingActivity extends BaseActivity {

    private boolean mIsDebugMode;
    private TextView cacheSize;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        setListeners();
    }

    private void setListeners() {
        findViewById(R.id.modifyPassword).setOnClickListener(this);
        findViewById(R.id.clearCache).setOnClickListener(this);
    }

    private void initView() {
        setTitle("设置");
        cacheSize = findViewById(R.id.cacheSize);
        cacheSize.setText(refreshCache());
        SharedPreferences spDebugMode = getSharedPreferences("debug_mode", 0);
        mIsDebugMode = spDebugMode.getBoolean(GlobalConstant.KEY_DEBUG_MODE, false);
        setDebug();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.modifyPassword:
                startActivity(new Intent(SettingActivity.this, ChangePasswordActivity.class));
                break;
            case R.id.clearCache:
                startActivityForResult(new Intent(this, AppCacheActivity.class), GlobalConstant.REQUEST_CODE_APP_CACHE_ACTIVITY);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case GlobalConstant.REQUEST_CODE_APP_CACHE_ACTIVITY:
                    cacheSize.setText(refreshCache());
                    break;
            }
        }
    }

    public String refreshCache() {
        String cacheSize = null;
        try {
            cacheSize = DataCleanManager.getTotalCacheSize(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (cacheSize == null) {
            return "0.0MB";
        } else {
            return cacheSize;
        }
    }

    private void setDebug() {
        mTitleView.setOnClickListener(new View.OnClickListener() {

            int count = 0;
            long startTime = 0;

            @Override
            public void onClick(View v) {
                final SharedPreferences spDebugMode = getSharedPreferences("debug_mode", 0);
                if (!spDebugMode.getBoolean(GlobalConstant.KEY_OPEN_DEBUG_RELEASE,false)){
                    return;
                }
                if (mIsDebugMode) {
                    startActivity(new Intent(SettingActivity.this, DebugActivity.class));
                    return;
                }
                long time = System.currentTimeMillis();
                if (time - startTime > 1000) {
                    count = 0;
                    startTime = time;
                }
                count++;
                if (count >= 5) {
                    count = 0;
                    DialogFragment debugDialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag("debugDialog");
                    if (debugDialogFragment == null) {
                        debugDialogFragment = new DebugDialogFragment().setDebugDialogFragmentListener(new DebugDialogFragment.DebugDialogFragmentListener() {
                            @Override
                            public void onSkipDebugActivity() {
                                mIsDebugMode = true;
                                SharedPreferences.Editor ed = spDebugMode.edit();
                                ed.putBoolean(GlobalConstant.KEY_DEBUG_MODE, mIsDebugMode);
                                ed.commit();
                                startActivity(new Intent(SettingActivity.this, DebugActivity.class));
                            }
                        });
                    }
                    debugDialogFragment.show(getSupportFragmentManager(), "debugDialog");
                }
            }
        });
    }
}
